package com.neusoft.demo.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.neusoft.demo.dao.GouwucheMapper;
import com.neusoft.demo.po.Gouwuche;
import com.neusoft.demo.utils.SqlSessionFactoryUtil;

@WebServlet("/GouWuCheServlet")
public class GouWuCheServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/*
		 * 由于没有登录，这里用户静态赋值：20
		 * 参考https://blog.csdn.net/u013475360/article/details/53376684
		 * 
		 * //返回与当前request相关联的session，如果没有则在服务器端创建一个 HttpSession httpSession
		 * =request.getSession(); //获取session httpSession.getAttribute("uid");
		 */
		Integer uid = 20;
		// 创建SqlSession对象
		SqlSession session = SqlSessionFactoryUtil.getSqlSessionFactory().openSession();
		// 生成代理类
		GouwucheMapper gouwucheMapper = session.getMapper(GouwucheMapper.class);
		// 获得商品的商品信息
		List<Gouwuche> list = gouwucheMapper.findGouwucheByUid(uid);

		// 在跳转页面
		request.setAttribute("list", list);
		request.getRequestDispatcher("gouwuche.jsp").forward(request, response);
		// 释放资源
		session.close();
	}

}
